const {setStorage,getStorage}  = require('../../utils/localStroage')
Page({
  data: {
    listing: [],
    done:[]
  },
  onLoad(){
    var listing = this.data.listing;
    var done = this.data.done;
    listing = getStorage("run")||[];
    done = getStorage("runed")||[]
    this.setData({
      listing,
      done
    })
  },
  toAdd(e) {
    var listing = this.data.listing;
    var value = e.detail.value
    if (!listing.includes(value) && value != "") {
      listing.unshift(value)
      value = ""
    }
    this.local()
    this.setData({
      listing,
      value
    })
  },
  toDone(e){
    var listing = this.data.listing;
    var index = listing.indexOf(e.currentTarget.dataset.index)
    var res = listing.splice(index,1)
    var done = this.data.done;
    done.unshift(...res)
    this.local()
    this.setData({
      listing,
      done
    })
  },
  toList(e){
    var done = this.data.done;
    var index = done.indexOf(e.currentTarget.dataset.index)
    var res = done.splice(index,1)
    var listing = this.data.listing;
    listing.unshift(...res);
    this.local()
    this.setData({
      listing,
      done
    })
  },
  local(){
    var listing = this.data.listing;
    var done = this.data.done;
    setStorage("run",listing);
    setStorage("runed",done)
  },
  toListDelete(e) {
    var index = e.currentTarget.dataset;
    var listing = this.data.listing;
    listing.splice(index,1)
    this.local()
    this.setData({
      listing
    })
  },
  toDoneDelete(e) {
    var index = e.currentTarget.dataset;
    var done = this.data.done;
    done.splice(index,1);
    this.local()
    this.setData({
      done
    })
  },
})